var request = require("request");

export class HTTPOperation {

    /**
     * 模拟get请求
     * @param url 
     */
    public getURL(url: string) { 

      
        return new Promise(function (resolve, reject) { 
            request("http://"+url, function (error, response, body) {
                if (!error && response.statusCode == 200) { 
                       resolve(body);
                }
                else{
                    reject(error);
                }
            }); 
        });
    }


    /**
     * 模拟post请求
     * @param url 
     * @param body 
     */
    public postURL(url:string,body?:any)
    {
        return new Promise(function (resolve, reject) {  
            request({
                url: "http://"+url,
                method: "post",//如果是post就涉及到跨域的问题了
                json: true,
                headers: {
                    "content-type": "application/json",
                },
                body: body
            }, function (error, response, body) {
                if (!error && response.statusCode == 200) {
                   // console.log(body);
                    resolve(body);
                }
                else{
                    reject(error);
                }
            });  
        });
    }





}